// pages/editInfo/info.js
var app = getApp();
Page({
  data:{
    loginImg:'',
    loginName:'',
    aliasName:'',
    phoneNumber:'',
    imgId:'',
    loading:false
  },
  onLoad:function(options){
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    that.setData({
      loginImg:wx.getStorageSync('loginInfo').image.absoluteImagePath,
      loginName:wx.getStorageSync('loginInfo').user.loginName,
      aliasName:wx.getStorageSync('loginInfo').user.aliasName
    })
    wx.request({
      url: app.globalData.API + '/serverProvider/findInfo',
      data: {
        id:wx.getStorageSync('loginInfo').user.id
      },
      method: 'POST', 
      header: {
        "Cookie":wx.getStorageSync('sessionid')
      },
      success: function(res){
        // success
        if(res.data.code == 10000){
          var info = res.data.obj;
          if(info.loginName){
            that.setData({
              loginName:info.loginName
            })
          }
          if(info.serverProvider.name){
            that.setData({
              aliasName:info.serverProvider.name
            })
          }
          if(info.serverProvider.phoneNumber){
            that.setData({
              phoneNumber:info.serverProvider.phoneNumber
            })
          }
          if(info.image.absoluteImagePath){
            that.setData({
              loginImg:info.image.absoluteImagePath
            })
          }
        }
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    });
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  uploadImg:function(){
    var that = this;
    wx.chooseImage({
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: app.globalData.API + '/webUpload',
          header: {
            "Cookie":wx.getStorageSync('sessionid')
          },
          filePath: tempFilePaths[0],
          name: 'file',
          // formData:{
          //   'user': 'test'
          // },
          success: function(res){
            var fileData = JSON.parse(res.data);
            console.log(fileData);
            that.setData({
              loginImg:fileData[0].imagePath,
              // imgId:fileData[0].id
              imgId:fileData[0].id
            })
            //do something
          }
        })
      }
    })
  },
  formSubmit:function(e){
    var that = this;
    that.setData({
      loading: true
    })
    var datas = {};
    if(that.data.imgId != ''){
      datas = {
        id:wx.getStorageSync('loginInfo').user.id,
        "imageId": "20329",
	      "phoneNumber": e.detail.value.phone
      }
    }else{
      datas = {
        id:wx.getStorageSync('loginInfo').user.id,
	      "phoneNumber": e.detail.value.phone
      }
    }
    wx.request({
      url: app.globalData.API + '/serverProvider/updateUser',
      data: datas,
      method: 'POST', 
      header: {
        "Cookie":wx.getStorageSync('sessionid')
      },
      success: function(res){
        // success
        if(res.data.code == 10000){
          wx.showToast({
            title: '操作成功！',
            icon: 'success',
            duration: 2000,
            mask:true,
            success:function(){
              setTimeout(function(){
                wx.switchTab({
                  url: '/pages/user/info'
                })
              },2500)
            }
          })
        }
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
        that.setData({
          loading: false
        })
      }
    });
    console.log('form发生了submit事件，携带数据为：',e.detail.value);
  }
})